import React, { PropsWithChildren } from 'react';
import { Form, message, Select } from 'antd';
import { ModalForm, ProForm, ProFormText, } from '@ant-design/pro-components';
import { evaluationComposeV1ControllerCreate, evaluationComposeV1ControllerUpdate } from "@/services/auto/evaluationCompose";
import EvaluationSelect, { Value } from "@/components/EvaluationSelect";
import { EvaluationCategory } from "common/utils/data";

interface FormData extends Omit<API.EvaluationComposeCreateDto,
  'earbudsEvaluationUuid' | 'earbudsEvaluationDataGroup' | 'soundSourceEvaluationUuid' | 'soundSourceEvaluationDataGroup'
>{
  earbudsEvaluation: Value<true>;
  soundSourceEvaluation?: Value<true>;
}

interface Props {
  data?: API.EvaluationComposeIndexResult;
  reload?: () => void;
}

export default (props: PropsWithChildren<Props>) => {
  const { data, reload, children } = props;

  const [form] = Form.useForm<FormData>();

  return (
    <ModalForm
      layout="horizontal"
      width={800}
      labelCol={{style: {width: 60}}}
      modalProps={{
        maskClosable: false,
      }}
      form={form}
      onOpenChange={(val) => {
        if (val) {
          if (data?.uuid) {
            form.setFieldsValue({
              ...data,
              earbudsEvaluation: {
                evaluationUuid: data.earbudsEvaluationUuid,
                dataGroup: data.earbudsEvaluationDataGroup,
              },
              soundSourceEvaluation: {
                evaluationUuid: data.soundSourceEvaluationUuid,
                dataGroup: data.soundSourceEvaluationDataGroup,
              }
            });
          }
        } else {
          form.resetFields();
        }
      }}
      title={data?.uuid ? '系统编辑' : '添加系统'}
      trigger={children as JSX.Element}
      onFinish={(values) => {
        const {earbudsEvaluation, soundSourceEvaluation, ...restValues} = values;
        let service;
        const newValues: API.EvaluationComposeCreateDto = {
          ...restValues,
          earbudsEvaluationUuid: earbudsEvaluation.evaluationUuid!,
          earbudsEvaluationDataGroup: earbudsEvaluation.dataGroup!,
          soundSourceEvaluationUuid: soundSourceEvaluation?.evaluationUuid,
          soundSourceEvaluationDataGroup: soundSourceEvaluation?.dataGroup
        };
        if (data?.uuid) {
          service = evaluationComposeV1ControllerUpdate({uuid: data.uuid},newValues);
        } else {
          service = evaluationComposeV1ControllerCreate(newValues);
        }
        return service
          .then(() => {
            message.success('操作成功');
            reload?.();
            return true;
          })
          .catch(() => {
            return false;
          });
      }}
    >
      <ProFormText
        name="title"
        label="名称"
        rules={[{ required: true, message: '请输入' }]}
        fieldProps={{
          maxLength: 200,
        }}
      />
      <ProForm.Item name="earbudsEvaluation" label="耳机" required rules={[{validator: (_,value)=>{
        return EvaluationSelect.getValidator(value, true, true);
      }}]}>
        <EvaluationSelect category={EvaluationCategory.EARBUDS} withDataGroup />
      </ProForm.Item>
      <ProForm.Item name="soundSourceEvaluation" label="音源" rules={[{validator: (_,value)=>{
          return EvaluationSelect.getValidator(value, false, true);
        }}]}>
        <EvaluationSelect category={EvaluationCategory.SOUND_SOURCE} withDataGroup />
      </ProForm.Item>
    </ModalForm>
  );
};
